﻿@{
    ViewBag.Title = "SysGoodsList";
}
@section scripts
{
    <style>
        img {
            border:2px solid #808080;
        }
    </style>
    <link rel="stylesheet" href="../Scripts/kindeditor-4.1.10/themes/default/default.css" />
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/kindeditor-min.js"></script>
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/lang/zh_CN.js"></script>    
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
}

<form id="form1" method="post">
            @Html.Partial("~/Views/Modular/Toolbars.cshtml")
    <div id="tabs1" class="mini-tabs" activeIndex="0"  style="width:100%; height:100%; top: 0px; left: 0px;overflow:hidden;" plain="false"  buttons="#tabsButtons" onactivechanged="changeTabs">
        <div name="Base" title="商品资料" style="height:100%;overflow:hidden;">
            <table style="width:100%;margin-top:10px">
                <tr>
                    <td width="80px" height ="32px" align="right">
                        商品分类：</td>
                    <td width="200px" height ="32px">
                        <input id="FClassName" name="FClassName" class="mini-textbox "  style="width:200px;" required="true" TabIndex="1"/>
                    </td>
                    <td width="80px" height ="32px" align="right">
                        建议进价：</td>
                    <td width="200px" height ="32px">
                        <input id="FPurPrice" name="FPurPrice" class="mini-spinner" minValue="0" maxValue="9999999999" changeOnMousewheel ="false" 
                            required="true"  style="width:200px;" TabIndex="6" showbutton="false"  format="#,0.00"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="32px" align="right">
                        商品条码：</td>
                    <td width="200px" height ="32px">
                        <input id="FCode" name="FCode" class="mini-textbox "  style="width:200px;" required="true" TabIndex="2"/>
                    </td>
                    <td width="80px" height ="32px" align="right">
                        建议零售价：</td>
                    <td width="200px" height ="32px">
                        <input id="FPosPrice" name="FPosPrice" class="mini-spinner" minValue="0" maxValue="9999999999" changeOnMousewheel ="false" 
                            required="true"  style="width:200px;" TabIndex="7" showbutton="false"  format="#,0.00"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="32px" align="right">
                        商品名称：</td>
                    <td width="200px" height ="32px">
                        <input id="FName" name="FName" class="mini-textbox "  style="width:200px;" required="true" TabIndex="3"/>
                    </td>
                    <td width="80px" height ="32px" align="right">
                        助记码：</td>
                    <td width="200px" height ="32px">
                        <input id="FSpelling" name="FSpelling" class="mini-textbox "  style="width:200px;"  TabIndex="8"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="32px" align="right">
                        规格型号：</td>
                    <td width="200px" height ="32px">
                        <input id="FSpec" name="FSpec" class="mini-textbox "  style="width:200px;"  TabIndex="4"/>
                    </td>
                    <td width="80px" height ="32px" align="right">
                        生产产地：</td>
                    <td width="200px" height ="32px">
                        <input id="FMakeIn" name="FMakeIn" class="mini-textbox "  style="width:200px;"  TabIndex="9"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px" height ="32px" align="right">
                        单位：</td>
                    <td width="200px" height ="32px">
                        <input id="FUnitName" name="FUnitName" class="mini-textbox " style="width:200px;" required="true" TabIndex="5"/>
                    </td>
                    <td></td><td></td>
                </tr>
                <tr><td>&nbsp;</td><td></td><td></td><td></td></tr>
                <tr><td>&nbsp;</td><td></td><td></td><td></td></tr>
                <tr><td>&nbsp;</td><td></td><td></td><td></td></tr>
                <tr><td>&nbsp;</td><td></td><td></td><td></td></tr>
                <tr><td>&nbsp;</td><td></td><td></td><td></td></tr>
                <tr><td>&nbsp;</td><td></td><td></td><td></td></tr>
                <tr><td>&nbsp;</td><td></td><td></td><td></td></tr>
            </table>
            <br />
        </div>
        <div name="Spec" title="包装规格" style="height:100%;overflow:hidden;">
            @Html.Partial("~/Views/Modular/GoodEditBar.cshtml")
            <div id="Specgrid" class="mini-datagrid" style="width:100%;height:90%;" pageSize="15"
                    borderStyle="border:1;" url="GetSysGoodsSpecs" showPager="false" idField="FItemID"
                    allowCellSelect="true" allowCellEdit="true"showColumnsMenu="true"fitColumns="false"  
                    sortMode="client">
                <div property="columns">
                    <div type="checkcolumn" >选行</div>
                    <div header="FItemID" field="FItemID" width="0" visible="false"></div>
                    <div field="FCode" name="FCode" width="120" headeralign="center" allowsort="true">商品条码
                        <input property="editor" class="mini-textbox PopInput" style="width:100%;"/>
                    </div>
                    <div field="FSpec" name="FSpec" width="100" headeralign="center" allowsort="true">规格
                        <input property="editor" class="mini-textbox PopInput" style="width:100%;"/>
                    </div>
                    <div field="FUnitName" name="FUnitName" width="40" headeralign="center" allowsort="true">单位
                        <input property="editor" class="mini-textbox PopInput" style="width:100%;"/>
                    </div>
                    <div field="FConvert" name="FConvert" width="60" headeralign="center" allowsort="true">系数
                        <input property="editor" class="mini-textbox PopInput" style="width:100%;"/>
                    </div>
                    <div field="FPurPrice" name="FPurPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">建议进价
                        <input property="editor" class="mini-spinner PopInput" minValue="0" maxValue="99999999" value="0" style="width:100%;"/>
                    </div>
                    <div field="FPosPrice" name="FPosPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">建议零售价
                        <input property="editor" class="mini-spinner PopInput" minValue="0" maxValue="99999999" value="0" style="width:100%;"/>
                    </div>
                </div>
            </div>
        </div>
        <div name="DPic" id="DPic" title="展示图片" style="height:100%;overflow:hidden;">
            <table style= "width:100%; height:80%;">
                <tr>
                    <td class="auto-style1" colspan="10">
                        <div id="div_imglist" style="margin-left:30px;">
                                <img id="Image0" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image1" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image2" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image3" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image4" src="~/Content/Images/NoImage.jpg" Width="120" height="120" />
                        </div>
                    </td>            
                </tr>                
                <tr>
                    <td class="auto-style1" colspan="10">
                        <div id="div_imglist1" style="margin-left:30px;">
                                <img id="Image5" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image6" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image7" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image8" src="~/Content/Images/NoImage.jpg" Width="120" height="120" /> 
                                <img id="Image9" src="~/Content/Images/NoImage.jpg" Width="120" height="120" />
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="FItemID" class="mini-hidden" />
                        <input id="ImagFile0" name="ImagFile0" class="mini-hidden" />  
                        <input id="ImagFile1" name="ImagFile1" class="mini-hidden" />  
                        <input id="ImagFile2" name="ImagFile2" class="mini-hidden" />  
                        <input id="ImagFile3" name="ImagFile3" class="mini-hidden" />  
                        <input id="ImagFile4" name="ImagFile4" class="mini-hidden" />
                        <input id="ImagFile5" name="ImagFile5" class="mini-hidden" />  
                        <input id="ImagFile6" name="ImagFile6" class="mini-hidden" />  
                        <input id="ImagFile7" name="ImagFile7" class="mini-hidden" />  
                        <input id="ImagFile8" name="ImagFile8" class="mini-hidden" />  
                        <input id="ImagFile9" name="ImagFile9" class="mini-hidden" /> 
                    </td>
                </tr>
            </table>
        </div>
        <div id="DHtml" name="DHtml" title="商城详情" style="height:100%;overflow:hidden;">
            <textarea id="content" name="content" style="width:700px;height:400px;visibility:hidden;"></textarea>
        </div>
    </div>
</form>
<div id="tabsButtons">
    <div id="ck1" name="ck1"  class="mini-checkbox" style ="margin-right:20px" readOnly="false" value ="true" text="连续新增 "></div>
</div> 

<script>
    mini.parse();

    var editor1;
    $(function () {
        KindEditor.ready(function (K) {
            editor1 = K.create('textarea[name="content"]', {
                uploadJson: '../asp.net/upload_json.ashx?dirs=proImage&mSystemAppID=' + parent.window.getSystemAppID() + '&thumb=true' + '&iseDitor=true',
                fileManagerJson: '../asp.net/file_manager_json.ashx?dirs=proImage&mSystemAppID=' + parent.window.getSystemAppID(),
                afterBlur: function () { this.sync(); },
                allowFileManager: true
            });

            $.each($("img"), function (index, e) {
                BindImageClick(K, e);
            });
        });
    });

    function changeTabs(sender, tab) {
        if (sender.name == "DPic") {
            loadurl = editor1.uploadJson = '../asp.net/upload_json.ashx?dirs=proImage&mSystemAppID=' + parent.window.getSystemAppID() + '&thumb=true';
            manageurl = editor1.fileManagerJson = '../asp.net/file_manager_json.ashx?dirs=proImage&mSystemAppID=' + parent.window.getSystemAppID();
        }
        else if (sender.name == "DHtml") {
            loadurl = editor1.uploadJson = '../asp.net/upload_json.ashx?dirs=content&mSystemAppID=' + parent.window.getSystemAppID() + '&thumb=false';
            manageurl = editor1.fileManagerJson = '../asp.net/file_manager_json.ashx?dirs=content&mSystemAppID=' + parent.window.getSystemAppID();
        }
    }

    function BindImageClick(K, e) {
        K('#' + $(e).attr("id")).click(function (K) {
            var imgIndex = $(this).attr("id").replace("Image", "");
            var url = $(this).attr('src');
            if (url.toLowerCase() != "/content/images/noimage.jpg") {
                var cfg = confirm("你确定要删除当前图片信息？");
                if (!cfg) {
                    return;
                }
                else {
                    $(this).attr('src', "/content/Images/NoImage.jpg");
                    mini.getbyName("ImagFile" + imgIndex).setValue("");
                    return;
                }
            }
            editor1.loadPlugin('image', function () {
                editor1.plugin.imageDialog({
                    //imageUrl: "",
                    clickFn: function (url, title, width, height, border, align) {
                        var path = ImageFilePath(url);
                        mini.getbyName("ImagFile" + imgIndex).setValue(path);
                        $("#Image" + imgIndex).attr('src', path);
                        editor1.hideDialog();
                    }
                });
            });
        });
    }

    function ImageFilePath(url) {
        var result = "";
        $.ajax({
            url: "/GetItem/GetImageFilePath",
            data: { Url: url },
            type: "post",
            async: false,
            success: function (data) {
                result = data;
            },
            failure: function () {
                result = "";
            },
            error: function (jqXHR, textStatus, errorThrown) {
                return "";
            }
        });
        return result;
    }

    function FormShown() {
    }

    //加载资料
    function SetInfoData(data) {
        $(".mini-tabs-body").css("overflow", "hidden");
        if (data.action == "new") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            mini.getbyName("FItemID").setValue('0');
            mini.getbyName("ck1").setValue(true);
            NewInfo();
        }
        else {
            mini.getbyName("FItemID").setValue(data.FItemID);
            $.post("GetSysGood", { json: "[{'FItemID':'" + data.FItemID + "','mSystemAppID':''}]" }, function (data) {
                var form = new mini.Form("form1");
                var mdata = mini.decode(data);
                form.setData(mdata.data[0]);
                var htmlstr = decodeURIComponent(mdata.data[0].FContent);
                editor1.insertHtml(htmlstr);
                if (mdata.data[0].FProImage != "" && mdata.data[0].FProImage != null) {
                    var ImgFile = mdata.data[0].FProImage.split("$");
                    if (ImgFile.length > 0) {
                        $.each(ImgFile, function (key, item) {
                            if (item != "") {
                                item = ImageFilePath(item);
                                $("#Image" + key).attr("src", item);
                                mini.getbyName("ImagFile" + key).setValue(item);
                            }
                        });
                    }
                }
                mini.getbyName("ck1").setValue(true);
                    var Specgrid = mini.get("Specgrid");
                    Specgrid.load({ json: mdata.data[0].FItemID, mSystemAppID: '' });

                for (var i = 1; i < mdata.data.length; i++) {
                    var row = {
                        "FCode": mdata.data[i].FCode, "FSpec": mdata.data[i].FSpec, "FUnitName": mdata.data[i].FUnitName,
                        "FConvert": mdata.data[i].FConvert,"FPurPrice":mdata.data[i].FPurPrice,"FPosPrice":mdata.data[i].FPosPrice
                    }
                    var grid = mini.get("Specgrid");
                    grid.addRow(row, 0);
                }

                for (var i = 0; i < 10; i++) {
                    if ("~/Content/Images/NoImage.jpg" != mini.getbyName("ImagFile" + i).getValue()) {
                        mini.getbyName("ImagFile" + i).setValue($("#Image" + i).attr("src"));
                    }
                }
            });
        }
    }

    //新增资料
    function NewInfo() {
        mini.getbyName("FClassName").setValue("");
        mini.getbyName("FUnitName").setValue("");
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("FName").setValue("");
        mini.getbyName("FSpec").setValue("");
        mini.getbyName("FCode").setValue("");
        mini.getbyName("FSpelling").setValue("");
        mini.getbyName("FPurPrice").setValue("0");
        mini.getbyName("FPosPrice").setValue("0");
        //var date = new Date();
        //var str = (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        // mini.getbyName("FModifyTime").setValue(str);
        //var Fnumber = "";//mini.getbyName("FClassNumber").getText();
        //$.post('NewGoodsInfo', { json: Fnumber }, function (data) {
        //    mini.getbyName("FNumber").setValue(data);
        //});
    }

    function GCode(s) {
        var REGX_HTML_ENCODE = /"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g
        return (typeof s != "string") ? s :
            s.replace(REGX_HTML_ENCODE,
                      function ($0) {
                          var c = $0.charCodeAt(0), r = ["&#"];
                          c = (c == 0x20) ? 0xA0 : c;
                          r.push(c); r.push(";");
                          return r.join("");
                      });
    };
    //保存资料
    function SaveInfo() {
        //mini.getbyName("FSystemAppID").setValue(parent.window.getSystemAppID());
        mini.getbyName("SaveInfo").disable();
        var form = new mini.Form("#form1");
        form.validate();
        if (form.isValid() == false) {
            mini.getbyName("SaveInfo").enable();
            return;
        }
        var data = form.getData(true, false);

        var Specgrid = mini.get("Specgrid");
        var rows = Specgrid.getData();
        var specJson = mini.encode(rows);
        //var detailHtml = GCode($("#contentHtml").val());
        data.contentHtml = encodeURIComponent($("#content").val());
        //data.contentHtml = detailHtml;
        var json = mini.encode(data);
        $.post("SaveSysGoodsInfo", { json: json, specjson: specJson, mSystemAppID: '' }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 }, function () {
                    mini.alert("保存完成");
                });
                NewInfo();
                if (mini.getbyName("ck1").getChecked() == false)
                    CloseWindow();
                else
                    mini.getbyName("FCode").focus();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SaveInfo").enable();
        });
    }

    function addSpecInfo() {
        var grid = mini.get("Specgrid");
        var r = grid.getData().length + 1;
        var unitName = mini.get("FUnitName").getValue();
        var row = { FItemID: 0, FSpec: '', FUnitName: unitName, FConvert:1, FPurPrice:mini.getbyName("FPurPrice").getValue(), FPosPrice:mini.getbyName("FPosPrice").getValue() };
        grid.addRow(row, r);

        grid.cancelEdit();
        grid.beginEditRow(r);
    }

    function removeSpecInfo() {
        var grid = mini.get("Specgrid");
        var row = grid.getSelected();
        var FSystemAppID = '';//parent.window.getSystemAppID();
        if (row) {
            mini.confirm("确定要删除选中的记录吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        $.post("RemoveSysGoodsInfo", { json: row.FItemID, mSystemAppID: FSystemAppID }, function (data) {
                            var ret = mini.decode(data);
                            if (ret.status == 1) {
                                grid.reload();
                            }
                            else {
                                grid.reload();
                            }
                        });
                    }
                });
        } else {
            mini.alert("请选中一条记录");
        }
    }



</script>

